﻿Imports System.Data
Imports System.Data.SqlClient
Public Class Frm_Paiement_Clients
    Public num = -1

    Public Sub vider()
        List_paye.Items.Clear()
        List_requi.Items.Clear()
        txt_remarq_requi.Clear()
        txt_remarq_paye.Clear()
        txt_mont_requi.Clear()
        txt_mont_paye.Clear()
        txt_remis.Text = 0
        txt_rest.Clear()
        txt_total_req.Clear()
        txt_total_paye.Clear()
        txt_client.Clear()
        txt_date_evenement.Clear()
        txt_lieu.Clear()

    End Sub

    Public Sub reste()

        If txt_total_req.Text <> "" And txt_total_paye.Text <> "" Then
            txt_rest.Text = CDbl(txt_total_req.Text) - CDbl(txt_total_paye.Text)
        End If

    End Sub
    Public Sub total_mont_loc()
        If Radio_loc.Checked = True Then

            If Con.State = ConnectionState.Open Then
                cnClose()
            End If
            Dim k As Integer = 0
            Try
                Cmd = New SqlCommand("Select * From Clients where Location='Oui'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()

                    If Dr("Nom_entreprise") = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    End If
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try

            Try

                Cmd = New SqlCommand("select * from Clients_Demande_Loc where Id_Client =" & num & " and N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                If Dr.HasRows Then
                    Dr.Close()
                    Cmd = New SqlCommand("select sum(Quantite * Prix) as sume from Clients_Equi_Loc,Clients_Demande_Loc where Clients_Equi_Loc.N_demande=Clients_Demande_Loc.N_demande and Id_Client =" & num & " and Clients_Demande_Loc.N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                    Dr = Cmd.ExecuteReader
                    Dr.Read()
                    txt_mont_requi.Text = Dr("sume")
                Else
                    txt_mont_requi.Text = 0
                End If
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            '-------------------------------------------------------------------------------------------------------------------
        ElseIf Radio_Eve.Checked = True Then

            If Con.State = ConnectionState.Open Then
                cnClose()
            End If
            Dim a As Double = 0
            Dim b As Double = 0

            Try
                Cmd = New SqlCommand("select * from Clients_Equip where id_dem=" & id_event, Con)
                open()
                Dr = Cmd.ExecuteReader()
                If Dr.HasRows Then

                    Dr.Close()
                    Cmd = New SqlCommand("select sum(Prix_Total) as sume from Clients_Equip where id_dem=" & id_event, Con)
                    Dr = Cmd.ExecuteReader
                    Dr.Read()
                    a = Dr("sume")
                End If
                Dr.Close()
                cnClose()

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try

            '_________
            Try
                Cmd = New SqlCommand("select Prix_salle from Clients_Demande where id_dem=" & id_event, Con)
                open()
                Dr = Cmd.ExecuteReader()
                If Dr.HasRows Then
                    Dr.Read()
                    b = Dr("Prix_salle")
                End If
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            txt_mont_requi.Text = a + b
        End If
    End Sub
    Public Sub total_paye()
        If Radio_loc.Checked = True Then
            If Con.State = ConnectionState.Open Then
                cnClose()
            End If
            Dim k As Integer = 0
            Try
                Cmd = New SqlCommand("Select * From Clients where Location='Oui'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()

                    If Dr("Nom_entreprise") = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    End If
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try

            Try
                Cmd = New SqlCommand("select * from Clients_Demande_Loc,Clients_Paiement_loc where Clients_Paiement_loc.N_demande=Clients_Demande_Loc.N_demande and type_paiement='Paye' and Id_Client =" & num & " and Clients_Paiement_loc.N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                open()
                Dr = Cmd.ExecuteReader
                Dr.Read()
                If Dr.HasRows Then
                    Dr.Close()
                    Cmd = New SqlCommand("select sum(Montant) as sume from Clients_Demande_Loc,Clients_Paiement_loc where Clients_Paiement_loc.N_demande=Clients_Demande_Loc.N_demande and type_paiement='Paye' and Id_Client =" & num & " and Clients_Paiement_loc.N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                    Dr = Cmd.ExecuteReader
                    Dr.Read()
                    txt_total_paye.Text = Dr("sume")
                Else
                    txt_total_paye.Text = 0

                End If


                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            '---------------------------------------------------------------------------------------------------
        ElseIf Radio_Eve.Checked = True Then

            Try
                Cmd = New SqlCommand("select * from Clients_Paiement_demande where type_paiement='Paye' and Id_demande=" & id_event, Con)
                open()
                Dr = Cmd.ExecuteReader
                Dr.Read()
                If Dr.HasRows Then
                    Dr.Close()
                    Cmd = New SqlCommand("select sum(Montant) as sume from Clients_Paiement_demande where type_paiement='Paye' and Id_demande=" & id_event, Con)
                    Dr = Cmd.ExecuteReader
                    Dr.Read()
                    txt_total_paye.Text = Dr("sume")
                Else
                    txt_total_paye.Text = 0

                End If
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
        End If
    End Sub
    Public Sub total_requis()
        If Radio_loc.Checked = True Then
            Dim a As Double = 0
            Dim b As Double = 0
            If Con.State = ConnectionState.Open Then
                cnClose()
            End If
            Dim k As Integer = 0
            Try
                Cmd = New SqlCommand("Select * From Clients where Location='Oui'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()

                    If Dr("Nom_entreprise") = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    End If
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try

            Try
                Cmd = New SqlCommand("select * from Clients_Demande_Loc,Clients_Paiement_loc where Clients_Paiement_loc.N_demande=Clients_Demande_Loc.N_demande and type_paiement='Requis' and Id_Client =" & num & " and Clients_Paiement_loc.N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                open()
                Dr = Cmd.ExecuteReader
                Dr.Read()
                If Dr.HasRows Then
                    Dr.Close()
                    Cmd = New SqlCommand("select sum(Montant) as sume from Clients_Demande_Loc,Clients_Paiement_loc where Clients_Paiement_loc.N_demande=Clients_Demande_Loc.N_demande and type_paiement='Requis' and Id_Client =" & num & " and Clients_Paiement_loc.N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                    Dr = Cmd.ExecuteReader
                    Dr.Read()
                    a = Dr("sume")

                End If


                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            Try
                Cmd = New SqlCommand("select * from Clients_Demande_Loc,Clients_Paiement_loc where Clients_Paiement_loc.N_demande=Clients_Demande_Loc.N_demande and type_paiement='Requis' and Id_Client =" & num & " and Clients_Paiement_loc.N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                open()
                Dr = Cmd.ExecuteReader
                Dr.Read()
                If Dr.HasRows Then
                    Dr.Close()
                    Cmd = New SqlCommand("select sum(Remis) as sume from Clients_Demande_Loc,Clients_Paiement_loc where Clients_Paiement_loc.N_demande=Clients_Demande_Loc.N_demande and type_paiement='Requis' and Id_Client =" & num & " and Clients_Paiement_loc.N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                    Dr = Cmd.ExecuteReader
                    Dr.Read()
                    b = Dr("sume")

                End If


                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            txt_total_req.Text = a - b

            '---------------------------------------------------------------------------------------------------
        ElseIf Radio_Eve.Checked = True Then

            Dim a As Double = 0
            Dim b As Double = 0

            Try
                Cmd = New SqlCommand("select * from Clients_Paiement_demande where type_paiement='Requis' and Id_demande=" & id_event, Con)
                open()
                Dr = Cmd.ExecuteReader
                Dr.Read()
                If Dr.HasRows Then
                    Dr.Close()
                    Cmd = New SqlCommand("select sum(Montant) as sume from Clients_Paiement_demande where type_paiement='Requis' and Id_demande=" & id_event, Con)
                    Dr = Cmd.ExecuteReader
                    Dr.Read()
                    a = Dr("sume")

                End If


                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            Try
                Cmd = New SqlCommand("select * from Clients_Paiement_demande where type_paiement='Requis' and Id_demande=" & id_event, Con)
                open()
                Dr = Cmd.ExecuteReader
                Dr.Read()
                If Dr.HasRows Then
                    Dr.Close()
                    Cmd = New SqlCommand("select sum(Remis) as sume from Clients_Paiement_demande where type_paiement='Requis' and Id_demande=" & id_event, Con)
                    Dr = Cmd.ExecuteReader
                    Dr.Read()
                    b = Dr("sume")

                End If


                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            txt_total_req.Text = a - b
        End If
    End Sub
    Public Sub affiche_paye()
        If Radio_loc.Checked = True Then
            If Con.State = ConnectionState.Open Then
                cnClose()
            End If
            Dim k As Integer = 0

            txt_remarq_paye.Clear()
            Try
                Cmd = New SqlCommand("Select * From Clients where Location='Oui'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()
                    If Dr("Nom_entreprise") = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    End If
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try

            Try
                List_paye.Items.Clear()
                Cmd = New SqlCommand("select * from Clients_Paiement_loc,Clients_Demande_Loc where Clients_Paiement_loc.N_demande=Clients_Demande_Loc.N_demande and type_paiement='Paye' and Id_Client =" & num & "  and Clients_Paiement_loc.N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()
                    Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Montant"), Dr("Date_m"), Dr(5), Dr(0), Dr("idPai"), Dr("typePaiement")})
                    List_paye.Items.Add(Line)
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            '------------------------------------------------------------------------------------------------
        ElseIf Radio_Eve.Checked = True Then

            Try
                List_paye.Items.Clear()
                Cmd = New SqlCommand("select * from Clients_Paiement_demande where type_paiement='Paye' and Id_demande=" & id_event, Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()
                    Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Montant"), Dr("Date_m"), Dr(4), Dr(0), Dr("idPai"), Dr("typePaiement")})
                    List_paye.Items.Add(Line)
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
        End If
    End Sub
    Public Sub affiche_requis()
        If Radio_loc.Checked = True Then
            If Con.State = ConnectionState.Open Then
                cnClose()
            End If
            txt_remis.Text = 0
            txt_remarq_requi.Clear()
            Try
                Cmd = New SqlCommand("Select * From Clients where Location='Oui'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()

                    If Dr("Nom_entreprise") = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = comb_client.SelectedItem.ToString Then
                        num = Dr("Id_Client")
                    End If
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try

            Try
                List_requi.Items.Clear()
                Cmd = New SqlCommand("select * from Clients_Paiement_loc,Clients_Demande_Loc where Clients_Paiement_loc.N_demande=Clients_Demande_Loc.N_demande and type_paiement='Requis' and Id_Client =" & num & "  and Clients_Paiement_loc.N_demande='" & Comb_n_demande.SelectedItem & "'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()
                    Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Montant"), Dr("Date_m"), Dr("Remis"), Dr(5), Dr(0)})
                    List_requi.Items.Add(Line)
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            '----------------------------------------------------------------------------------------------------
        ElseIf Radio_Eve.Checked = True Then
            Try
                List_requi.Items.Clear()
                Cmd = New SqlCommand("select * from Clients_Paiement_demande where type_paiement='Requis' and Id_demande=" & id_event, Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()
                    Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Montant"), Dr("Date_m"), Dr("Remis"), Dr(4), Dr(0)})
                    List_requi.Items.Add(Line)
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
        End If
    End Sub

    Private Sub Frm_Paiement_Clients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbTypePay.SelectedIndex = 0
    End Sub
    Dim idPai As Integer
    Public Sub ajoutTypePaiement()
        'Tarik
        Dim idNomin As Integer 'pour stocker la valeur de l'idNominatif ajouté
        If Not IsNothing(nomin.nom) Then
            Cmd.Parameters.Clear()
            'cette partie ajoute le nominatif et retourne l'id de ce dernier
            Cmd.CommandText = "ajouterNominatif"
            Cmd.CommandType = CommandType.StoredProcedure
            Cmd.Parameters.Add("@nom", SqlDbType.VarChar)
            Cmd.Parameters("@nom").Value = nomin.nom
            Cmd.Parameters.Add("@Prenom", SqlDbType.VarChar)
            Cmd.Parameters("@Prenom").Value = nomin.prenom
            Cmd.Parameters.Add("@idNomin", SqlDbType.Int)
            Cmd.Parameters("@idNomin").Direction = ParameterDirection.Output
            Cmd.ExecuteNonQuery()
            idNomin = Cmd.Parameters("@idNomin").Value
        End If

        'Ajouter le Chéque ou l'effet
        If cmbTypePay.SelectedIndex = 2 Then
            If Not IsNothing(effetObj.numEffet) Then
                Cmd = New SqlCommand("INSERT INTO [Effet] " & _
                                "VALUES(" & idPai & ",@numChe,'" & Format(effetObj.dteEffet, "MM-dd-yyyy") & "'," & nullPourDate(effetObj.dteRetrait) & ",'" &
                                Format(effetObj.dateEcheance, "MM-dd-yyyy") & "'," & convertToBit(cheq.nominatif) & ",@banqueDestinataire,@banqueDestinateur,'C')", Con)
                'MsgBox(cheq.dteRetra)
                Cmd.Parameters.Add("@numChe", SqlDbType.VarChar)
                Cmd.Parameters("@numChe").Value = effetObj.numEffet

                Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                Cmd.Parameters("@banqueDestinataire").Value = effetObj.banqueDestinataire

                Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                Cmd.Parameters("@banqueDestinateur").Value = effetObj.banqueDestinateur

                Cmd.ExecuteNonQuery()
                If Not IsNothing(nomin.nom) Then
                    Cmd.CommandText = "insert into nominEffet values(" & idNomin & "," & idPai & ")"
                    Cmd.ExecuteNonQuery()
                End If


                effetObj = Nothing
                nomin = Nothing
                idNomin = 0
                idPai = 0
            End If
        ElseIf cmbTypePay.SelectedIndex = 1 Then
            If Not IsNothing(cheq.numChe) Then
                Cmd = New SqlCommand("INSERT INTO [cheques] " & _
                                 "VALUES(" & idPai & ",@numChe," & convertToBit(cheq.nominatif) & "," & nullPourDate(cheq.dteRetra) & ",'" & Format(cheq.dateChequ, "MM-dd-yyyy") &
                                 "',@banqueDestinataire,@banqueDestinateur,'C')", Con)
                'MsgBox(cheq.dteRetra)
                Cmd.Parameters.Add("@numChe", SqlDbType.VarChar)
                Cmd.Parameters("@numChe").Value = cheq.numChe

                Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                Cmd.Parameters("@banqueDestinateur").Value = cheq.banqueDestinateur

                Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                Cmd.Parameters("@banqueDestinataire").Value = cheq.banqueDestinataire
                Cmd.ExecuteNonQuery()

                If Not IsNothing(nomin.nom) Then
                    Cmd.CommandText = "insert into nominCheq values(" & idPai & "," & idNomin & ")"
                    Cmd.ExecuteNonQuery()
                End If
                cheq = Nothing
                nomin = Nothing
                idNomin = 0
                idPai = 0
            End If
        ElseIf cmbTypePay.SelectedIndex = 3 Then
            If Not IsNothing(objVirement.numCompte) Then
                Cmd = New SqlCommand("INSERT INTO [virement] " &
                                    "VALUES (" & idPai & ",@numCompte,@banqueDestinataire, " &
                                    "@banqueDestinateur,'" & Format(objVirement.dateVirement, "MM-dd-yyyy") & "',@nom,'C')", Con)

                Cmd.Parameters.Add("@numCompte", SqlDbType.VarChar)
                Cmd.Parameters("@numCompte").Value = objVirement.numCompte
                Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                Cmd.Parameters("@banqueDestinataire").Value = objVirement.banqueDestinataire
                Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                Cmd.Parameters("@banqueDestinateur").Value = objVirement.banqueDestinateur
                Cmd.Parameters.Add("@nom", SqlDbType.VarChar)
                Cmd.Parameters("@nom").Value = objVirement.nom

                open()
                Cmd.ExecuteNonQuery()
                cnClose()
                objVirement = Nothing
            End If
        End If
        'End
    End Sub
    Private Sub btn_ajouter_paye_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouter_paye.Click
        Try
            If List_paye.SelectedItems.Count <= 0 Then
                If Radio_loc.Checked = True Then
                    Try
                        Cmd = New SqlCommand("Select * From Clients where Location='Oui'", Con)
                        open()
                        Dr = Cmd.ExecuteReader()
                        Do While Dr.Read()

                            If Dr("Nom_entreprise") = comb_client.SelectedItem.ToString Then
                                num = Dr("Id_Client")
                            ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = comb_client.SelectedItem.ToString Then
                                num = Dr("Id_Client")
                            End If
                        Loop
                        Dr.Close()
                        cnClose()
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try

                    If txt_mont_requi.Text <> "" And comb_client.Text <> "" And Comb_n_demande.Text <> "" Then
                        ''tarik
                        'If cmbTypePay.SelectedIndex = 1 Then
                        '    If IsNothing(cheq.numChe) Then
                        '        MsgBox("Vous n'avez pas rempli le Chéque!!!", MsgBoxStyle.Critical, "Erreur")
                        '        Exit Sub
                        '    End If
                        'End If
                        'If cmbTypePay.SelectedIndex = 2 Then
                        '    If IsNothing(effetObj.numEffet) Then
                        '        MsgBox("Vous n'avez pas rempli l'Effet!!!", MsgBoxStyle.Critical, "Erreur")
                        '        Exit Sub
                        '    End If
                        'End If

                        'If cmbTypePay.SelectedIndex = 3 Then
                        '    If IsNothing(objVirement.numCompte) Then
                        '        MsgBox("Vous n'avez pas rempli le virement!!!", MsgBoxStyle.Critical, "Erreur")
                        '        Exit Sub
                        '    End If
                        'End If
                        ''End

                        If MsgBox("Vous Voulez Vraiment Ajouter ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Ajouter") = MsgBoxResult.Yes Then

                            ''Tarik
                            ''Ajouter le paiement dans la table paiement qui a une relation avec le Chéque et l'effet
                            'Cmd = New SqlCommand("INSERT INTO [paiements]([montant],[type])VALUES(@montant,@type)", Con)
                            'Cmd.Parameters.Add("@montant", SqlDbType.Float)
                            'Cmd.Parameters("@montant").Value = Math.Round(CDbl(txt_mont_paye.Text), 2)
                            'Cmd.Parameters.Add("@type", SqlDbType.VarChar)
                            'Cmd.Parameters("@type").Value = cmbTypePay.Text
                            'open()
                            'Cmd.ExecuteNonQuery()

                            'Cmd.CommandText = "select @@IDENTITY"
                            'idPai = Cmd.ExecuteScalar
                            ''End 

                            Cmd = New SqlCommand("insert into Clients_Paiement_loc (N_demande,Montant,Date_m,Remarque,type_paiement,typePaiement,idPai)" & _
                            "values (@N_demande,@Montant,@Date_m,@Remarque,@type_paiement,'" & cmbTypePay.Text & "'," & idPai & ")", Con)


                            Cmd.Parameters.Add("@N_demande", SqlDbType.VarChar).Value = Comb_n_demande.SelectedItem.ToString
                            Cmd.Parameters.Add("@Montant", SqlDbType.Float).Value = CDbl(txt_mont_paye.Text)
                            Cmd.Parameters.Add("@Date_m", SqlDbType.Date).Value = Date_paye.Value.Date
                            Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq_paye.Text
                            Cmd.Parameters.Add("@type_paiement", SqlDbType.VarChar).Value = "Paye"
                            Cmd.ExecuteNonQuery()

                            'ajoutTypePaiement()

                            cnClose()
                            MsgBox("Montant Ajouté", MsgBoxStyle.Information, "Réussi !")
                            txt_mont_paye.Clear()
                            txt_remarq_paye.Clear()
                            affiche_paye()
                            total_paye()
                            reste()


                        End If
                    End If
                    '-----------------------------------------------------------------------
                ElseIf Radio_Eve.Checked = True Then

                    If txt_mont_paye.Text <> "" And txt_client.Text <> "" And txt_date_evenement.Text <> "" Then
                        'tarik
                        If cmbTypePay.SelectedIndex = 1 Then
                            If IsNothing(cheq.numChe) Then
                                MsgBox("Vous n'avez pas rempli le Chéque!!!", MsgBoxStyle.Critical, "Erreur")
                                Exit Sub
                            End If
                        End If
                        If cmbTypePay.SelectedIndex = 2 Then
                            If IsNothing(effetObj.numEffet) Then
                                MsgBox("Vous n'avez pas rempli l'Effet!!!", MsgBoxStyle.Critical, "Erreur")
                                Exit Sub
                            End If
                        End If

                        If cmbTypePay.SelectedIndex = 3 Then
                            If IsNothing(objVirement.numCompte) Then
                                MsgBox("Vous n'avez pas rempli le virement!!!", MsgBoxStyle.Critical, "Erreur")
                                Exit Sub
                            End If
                        End If
                        'End

                        If MsgBox("Vous Voulez Vraiment Ajouter ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Ajouter") = MsgBoxResult.Yes Then


                            'Tarik
                            'Ajouter le paiement dans la table paiement qui a une relation avec le Chéque et l'effet
                            Cmd = New SqlCommand("INSERT INTO [paiements]([montant],[type])VALUES(@montant,@type)", Con)
                            Cmd.Parameters.Add("@montant", SqlDbType.Float)
                            Cmd.Parameters("@montant").Value = Math.Round(CDbl(txt_mont_paye.Text), 2)
                            Cmd.Parameters.Add("@type", SqlDbType.VarChar)
                            Cmd.Parameters("@type").Value = cmbTypePay.Text
                            open()
                            Cmd.ExecuteNonQuery()

                            Cmd.CommandText = "select @@IDENTITY"
                            idPai = Cmd.ExecuteScalar
                            'End 

                            Cmd = New SqlCommand("insert into Clients_Paiement_demande (Id_demande,Montant,Date_m,Remarque,type_paiement,typePaiement,idPai)" & _
                            "values (@N_demande,@Montant,@Date_m,@Remarque,@type_paiement,'" & cmbTypePay.Text & "'," & idPai & ")", Con)

                            Cmd.Parameters.Add("@N_demande", SqlDbType.VarChar).Value = id_event
                            Cmd.Parameters.Add("@Montant", SqlDbType.Float).Value = CDbl(txt_mont_paye.Text)
                            Cmd.Parameters.Add("@Date_m", SqlDbType.Date).Value = Date_paye.Value.Date
                            Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq_paye.Text
                            Cmd.Parameters.Add("@type_paiement", SqlDbType.VarChar).Value = "Paye"

                            Cmd.ExecuteNonQuery()
                            ajoutTypePaiement()

                            cnClose()
                            MsgBox("Montant Ajouté", MsgBoxStyle.Information, "Réussi !")


                            txt_mont_paye.Clear()
                            txt_remarq_paye.Clear()
                            affiche_paye()
                            total_paye()
                            reste()
                        End If
                    End If
                End If
            Else
                MsgBox("Un paiement est déjà séléctionné!!!", MsgBoxStyle.Critical, "Erreur")
                List_paye.Focus()
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub



    'Private Sub btn_modif_paye_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_modif_paye.Click

    '    If modif <> 0 Then
    '        If txt_mont_paye.Text <> "" Then
    '            If MsgBox("Vous Voulez Vraiment Modifier ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Modifier !") = MsgBoxResult.Yes Then
    '                Try
    '                    Cmd = New SqlCommand("update Clients_Paiement_loc set Montant=@Montant,Date_m=@Date_m,Remarque=@Remarque" & _
    '                    " Where id=" & modif, Con)
    '                    Cmd.Parameters.Add("@Montant", SqlDbType.Float).Value = txt_mont_paye.Text
    '                    Cmd.Parameters.Add("@Date_m", SqlDbType.Date).Value = Date_paye.Value.Date
    '                    Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq_paye.Text

    '                    open()
    '                    Cmd.ExecuteNonQuery()
    '                    cnClose

    '                    modif = 0
    '                    MsgBox("Montant Modifier ", MsgBoxStyle.Information, "R�ussi !")
    '                    txt_mont_paye.Clear()
    '                    txt_remarq_paye.Clear()
    '                    affiche_paye()
    '                    total_paye()
    '                    reste()
    '                Catch ex As Exception
    '                    MsgBox(ex.Message, MsgBoxStyle.Exclamation)
    '                    cnClose
    '                End Try
    '            End If
    '        End If
    '    End If


    'End Sub

    Private Sub List_paye_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_paye.SelectedIndexChanged
        Try
            If List_paye.SelectedIndices.Count > 0 Then
                Dim n As Integer
                If Radio_loc.Checked = True Then
                    With List_paye
                        ''tarik
                        'If Not String.IsNullOrEmpty(List_paye.SelectedItems(0).SubItems(4).Text) And Not String.IsNullOrWhiteSpace(List_paye.SelectedItems(0).SubItems(4).Text) Then
                        '    idPai = CInt(List_paye.SelectedItems(0).SubItems(4).Text)
                        'End If

                        For i As Integer = 0 To .SelectedIndices.Count - 1
                            n = CInt(.Items(.SelectedIndices(i)).SubItems(3).Text)
                            modif = CInt(.Items(.SelectedIndices(i)).SubItems(3).Text)
                        Next
                    End With
                    Try
                        Cmd = New SqlCommand("Select * From Clients_Paiement_loc where id =" & n, Con)
                        open()
                        Dr = Cmd.ExecuteReader()
                        Dr.Read()
                        If Dr.HasRows Then
                            txt_mont_paye.Text = Dr("Montant")
                            Date_paye.Text = Dr("Date_m")
                            txt_remarq_paye.Text = Dr("Remarque")
                            cmbTypePay.SelectedItem = Dr("typePaiement")
                        End If
                        Dr.Close()

                        ''Tarik
                        'remplirTypePai()
                        ''end
                        cnClose()
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                    '----------------------------------------------------------------------------------------------------
                ElseIf Radio_Eve.Checked = True Then
                    With List_paye
                        If .SelectedIndices.Count > 0 Then
                            For i As Integer = 0 To .SelectedIndices.Count - 1
                                n = CInt(.Items(.SelectedIndices(i)).SubItems(3).Text)
                                modif = CInt(.Items(.SelectedIndices(i)).SubItems(3).Text)
                                txt_mont_paye.Text = CDbl(.Items(.SelectedIndices(i)).SubItems(0).Text)
                                Date_paye.Text = .Items(.SelectedIndices(i)).SubItems(1).Text
                                txt_remarq_paye.Text = .Items(.SelectedIndices(i)).SubItems(2).Text
                            Next
                        End If
                    End With

                End If



                'tarik
                If Not String.IsNullOrEmpty(List_paye.SelectedItems(0).SubItems(4).Text) And Not String.IsNullOrWhiteSpace(List_paye.SelectedItems(0).SubItems(4).Text) Then
                    idPai = CInt(List_paye.SelectedItems(0).SubItems(4).Text)
                End If

                txt_mont_paye.Clear()
                txt_remarq_paye.Clear()

                Cmd = New SqlCommand("Select * From Clients_Paiement_demande where id =" & n, Con)
                open()
                Dr = Cmd.ExecuteReader()

                If Dr.Read() Then
                    txt_mont_paye.Text = Dr("Montant")
                    Date_paye.Text = Dr("Date_m")
                    txt_remarq_paye.Text = Dr("Remarque")
                    cmbTypePay.SelectedItem = Dr("typePaiement")
                End If
                Dr.Close()
                'Tarik
                remplirTypePai()
                'end
                cnClose()
            Else
                txt_mont_paye.Clear()
                txt_remarq_paye.Clear()
                cmbTypePay.SelectedIndex = 0
                cheq = Nothing
                effetObj = Nothing
                nomin = Nothing
                'end
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub
    Public Sub remplirTypePai()
        'remplir le cheque et le nominatif
        Dim typePai As String = ""
        If List_paye.SelectedItems(0).SubItems(5).Text = "Chéque" Then
            typePai = "nominCheq"
            Cmd = New SqlCommand("select * from cheques where idPai= " & idPai, Con)
            open()
            Dr = Cmd.ExecuteReader
            If Dr.HasRows Then
                Dr.Read()
                With cheq
                    .idPai = Dr.GetInt32(0)
                    .numChe = Dr.GetString(1)
                    .nominatif = Dr.GetBoolean(2)
                    .dteRetra = convertNullToDate(Dr(3))
                    .dateChequ = Dr(4)
                    .banqueDestinataire = Dr("banqueDestinataire")
                    .banqueDestinateur = Dr("banqueDestinateur")
                    .typePersonne = "C"
                End With
            End If
            Dr.Close()
        ElseIf List_paye.SelectedItems(0).SubItems(5).Text = "Effet" Then
            typePai = "nominEffet"
            Cmd = New SqlCommand("select * from Effet where idPai= " & idPai, Con)
            open()
            Dr = Cmd.ExecuteReader
            If Dr.HasRows Then
                Dr.Read()
                With effetObj
                    .idPai = Dr.GetInt32(0)
                    .numEffet = Dr.GetString(1)
                    .dteEffet = Dr(2)
                    .dteRetrait = convertNullToDate(Dr(3))
                    .dateEcheance = Dr(4)
                    .nominatif = Dr.GetBoolean(5)
                    .banqueDestinataire = Dr("banqueDestinataire")
                    .banqueDestinateur = Dr("banqueDestinateur")
                    .typePersonne = "C"
                End With
            End If
            Dr.Close()
        ElseIf List_paye.SelectedItems(0).SubItems(5).Text = "Virement" Then
            Cmd = New SqlCommand("select * from virement where idPai= " & idPai, Con)
            open()
            Dr = Cmd.ExecuteReader
            If Dr.HasRows Then
                Dr.Read()
                With objVirement
                    .idPai = Dr.GetInt32(0)
                    .numCompte = Dr.GetString(1)
                    .banqueDestinataire = Dr.GetString(2)
                    .banqueDestinateur = Dr.GetString(3)
                    .dateVirement = Dr(4)
                    .nom = Dr.GetString(5)
                    .typePersonne = "C"
                End With
            End If
            Dr.Close()
        End If
        If cheq.numChe <> Nothing Or effetObj.numEffet <> Nothing Then
            Cmd = New SqlCommand("select * from nominatifA " &
                                "where idNomin in (select " & typePai & ".idNomin from " & typePai & " where " & typePai & ".idPai = " & idPai & ")", Con)
            Dr = Cmd.ExecuteReader
            If Dr.HasRows Then
                Dr.Read()
                With nomin
                    .idNomin = Dr.GetInt32(0)
                    .idPai = cheq.idPai
                    .nom = Dr.GetString(1)
                    .prenom = Dr.GetString(2)
                End With
            End If
            Dr.Close()
            cnClose()
        End If
        'end remplissage
    End Sub
    Private Sub btn_supp_paye_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp_paye.Click
        Try
            If Radio_loc.Checked = True Then
                Dim n As Integer
                With List_paye
                    If .SelectedIndices.Count > 0 Then
                        For i As Integer = 0 To .SelectedIndices.Count - 1
                            n = CInt(.Items(.SelectedIndices(i)).SubItems(3).Text)
                        Next
                        If MsgBox("Voulez Vous Vraiment Supprimer", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Suppréssion !") = MsgBoxResult.Yes Then

                            Cmd = New SqlCommand("Delete From Clients_Paiement_loc Where id =" & n, Con)
                            open()
                            Cmd.ExecuteNonQuery()
                            'Tarik
                            'Supprimer le paiments d'héritage
                            Cmd = New SqlCommand("delete from paiements where idPai = " & idPai, Con)
                            Cmd.ExecuteNonQuery()
                            cnClose()

                            For Each it As ListViewItem In List_paye.SelectedItems
                                it.Selected = False
                            Next

                            modif = 0
                            MsgBox("Montant Supprimer ", MsgBoxStyle.Information, "Réussi !")
                            txt_mont_paye.Clear()
                            txt_remarq_paye.Clear()
                            affiche_paye()
                            total_paye()
                            reste()


                        End If
                    Else
                        MsgBox("Vous n'avez selectionner aucun ligne !", MsgBoxStyle.Critical, "Réssayer !")
                    End If

                End With
                '--------------------------------------------------------------------------------------------
            ElseIf Radio_Eve.Checked = True Then
                Dim n As Integer
                With List_paye
                    If .SelectedIndices.Count > 0 Then
                        For i As Integer = 0 To .SelectedIndices.Count - 1
                            n = CInt(.Items(.SelectedIndices(i)).SubItems(3).Text)
                        Next
                        If MsgBox("Voulez Vous Vraiment Supprimer", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Suppréssion !") = MsgBoxResult.Yes Then
                            Try
                                Cmd = New SqlCommand("Delete From Clients_Paiement_demande Where Id =" & n, Con)
                                open()
                                Cmd.ExecuteNonQuery()
                                'Tarik
                                'Supprimer le paiments d'héritage
                                Cmd = New SqlCommand("delete from paiements where idPai = " & idPai, Con)
                                Cmd.ExecuteNonQuery()
                                cnClose()

                                For Each it As ListViewItem In List_paye.SelectedItems
                                    it.Selected = False
                                Next

                                MsgBox("Montant Supprimer ", MsgBoxStyle.Information, "Réussi !")
                                txt_mont_paye.Clear()
                                txt_remarq_paye.Clear()
                                affiche_paye()
                                total_paye()
                                reste()

                            Catch ex As Exception
                                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                                cnClose()
                            End Try
                        End If
                    Else
                        MsgBox("Vous n'avez selectionner aucun ligne !", MsgBoxStyle.Critical, "R�ssayer !")
                    End If

                End With
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try

    End Sub

    Private Sub btn_ajouter_req_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouter_req.Click
        If Radio_loc.Checked = True Then

            If Con.State = ConnectionState.Open Then
                cnClose()
            End If


            If txt_mont_requi.Text <> "" And comb_client.Text <> "" And Comb_n_demande.Text <> "" And List_requi.Items.Count = 0 Then
                If MsgBox("Vous Voulez Vraiment Ajouter ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Ajouter") = MsgBoxResult.Yes Then
                    Try
                        Cmd = New SqlCommand("insert into Clients_Paiement_loc (N_demande,Montant,Date_m,Remarque,type_paiement,Remis)" & _
                        "values (@N_demande,@Montant,@Date_m,@Remarque,@type_paiement,@Remis)", Con)

                        Cmd.Parameters.Add("@N_demande", SqlDbType.VarChar).Value = Comb_n_demande.SelectedItem.ToString
                        Cmd.Parameters.Add("@Montant", SqlDbType.Float).Value = CDbl(txt_mont_requi.Text)
                        Cmd.Parameters.Add("@Date_m", SqlDbType.Date).Value = Date_requi.Value.Date
                        Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq_requi.Text
                        Cmd.Parameters.Add("@type_paiement", SqlDbType.VarChar).Value = "Requis"
                        Cmd.Parameters.Add("@Remis", SqlDbType.Float).Value = CDbl(txt_remis.Text)

                        open()
                        Cmd.ExecuteNonQuery()
                        cnClose()
                        MsgBox("Montant Ajouté", MsgBoxStyle.Information, "Réussi !")

                        txt_remis.Text = 0
                        txt_remarq_requi.Clear()
                        affiche_requis()

                        total_requis()
                        reste()

                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                End If

            End If
            '-------------------------------------------------------------------------
        ElseIf Radio_Eve.Checked = True Then

            If Con.State = ConnectionState.Open Then
                cnClose()
            End If


            If txt_mont_requi.Text <> "" And txt_client.Text <> "" And txt_date_evenement.Text <> "" And List_requi.Items.Count = 0 Then
                If MsgBox("Vous Voulez Vraiment Ajouter ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Ajouter") = MsgBoxResult.Yes Then
                    Try
                        Cmd = New SqlCommand("insert into Clients_Paiement_demande (Id_demande,Montant,Date_m,Remarque,type_paiement,Remis)" & _
                        "values (@N_demande,@Montant,@Date_m,@Remarque,@type_paiement,@Remis)", Con)

                        Cmd.Parameters.Add("@N_demande", SqlDbType.VarChar).Value = id_event
                        Cmd.Parameters.Add("@Montant", SqlDbType.Float).Value = CDbl(txt_mont_requi.Text)
                        Cmd.Parameters.Add("@Date_m", SqlDbType.Date).Value = Date_requi.Value.Date
                        Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq_requi.Text
                        Cmd.Parameters.Add("@type_paiement", SqlDbType.VarChar).Value = "Requis"
                        Cmd.Parameters.Add("@Remis", SqlDbType.Float).Value = CDbl(txt_remis.Text)

                        open()
                        Cmd.ExecuteNonQuery()
                        cnClose()
                        MsgBox("Montant Ajouté", MsgBoxStyle.Information, "Réussi !")

                        txt_remis.Clear()
                        txt_remarq_requi.Clear()

                        affiche_requis()

                        total_requis()
                        reste()

                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                End If
            End If

        End If

    End Sub

    'Private Sub btn_modif_req_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_modif_req.Click
    '    If modifR <> 0 Then
    '        If Radio_loc.Checked = True Then
    '            If txt_mont_requi.Text <> "" Then
    '                If MsgBox("Vous Voulez Vraiment Modifier ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Modifier !") = MsgBoxResult.Yes Then
    '                    Try
    '                        Cmd = New SqlCommand("update Clients_Paiement_loc set Montant=@Montant,Date_m=@Date_m,Remarque=@Remarque" & _
    '                        " Where id=" & modifR, Con)
    '                        Cmd.Parameters.Add("@Montant", SqlDbType.Float).Value = txt_mont_requi.Text
    '                        Cmd.Parameters.Add("@Date_m", SqlDbType.Date).Value = Date_requi.Value.Date
    '                        Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq_requi.Text

    '                        open()
    '                        Cmd.ExecuteNonQuery()
    '                        cnClose

    '                        modifR = 0
    '                        MsgBox("Montant Modifier ", MsgBoxStyle.Information, "R�ussi !")
    '                        txt_remis.Clear()
    '                        txt_remarq_requi.Clear()
    '                        affiche_requis()

    '                        total_requis()
    '                        reste()
    '                    Catch ex As Exception
    '                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
    '                        cnClose
    '                    End Try
    '                End If
    '            End If
    '            '---------------------------------------------------------------------------------------------------------
    '        ElseIf Radio_Eve.Checked = True Then
    '            If txt_mont_requi.Text <> "" Then
    '                If MsgBox("Vous Voulez Vraiment Modifier ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Modifier !") = MsgBoxResult.Yes Then
    '                    Try
    '                        Cmd = New SqlCommand("update Clients_Paiement_loc set Montant=@Montant,Date_m=@Date_m,Remarque=@Remarque" & _
    '                        " Where id=" & modifR, Con)
    '                        Cmd.Parameters.Add("@Montant", SqlDbType.Float).Value = txt_mont_requi.Text
    '                        Cmd.Parameters.Add("@Date_m", SqlDbType.Date).Value = Date_requi.Value.Date
    '                        Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq_requi.Text

    '                        open()
    '                        Cmd.ExecuteNonQuery()
    '                        cnClose

    '                        modifR = 0
    '                        MsgBox("Montant Modifier ", MsgBoxStyle.Information, "R�ussi !")
    '                        txt_remis.Clear()
    '                        txt_remarq_requi.Clear()
    '                        affiche_requis()

    '                        total_requis()
    '                        reste()
    '                    Catch ex As Exception
    '                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
    '                        cnClose
    '                    End Try
    '                End If
    '            End If
    '        End If
    '    End If
    'End Sub

    Private Sub btn_supp_req_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp_req.Click

        If Radio_loc.Checked = True Then
            Dim n As Integer
            With List_requi
                If .SelectedIndices.Count > 0 Then
                    For i As Integer = 0 To .SelectedIndices.Count - 1
                        n = CInt(.Items(.SelectedIndices(i)).SubItems(4).Text)
                    Next
                    If MsgBox("Voulez Vous Vraiment Supprimer", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Supprission !") = MsgBoxResult.Yes Then
                        Try
                            Cmd = New SqlCommand("Delete From Clients_Paiement_loc Where id =" & n, Con)
                            open()
                            Cmd.ExecuteNonQuery()
                            cnClose()

                            MsgBox("Montant Supprimer ", MsgBoxStyle.Information, "R�ussi !")
                            txt_remarq_requi.Clear()
                            txt_remis.Clear()
                            affiche_requis()

                            total_requis()
                            reste()
                        Catch ex As Exception
                            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                            cnClose()
                        End Try
                    End If
                Else
                    MsgBox("Vous n'avez selectionner aucun ligne !", MsgBoxStyle.Critical, "R�ssayer !")
                End If
            End With
            '---------------------------------------------------------------------
        ElseIf Radio_Eve.Checked = True Then
            Dim n As Integer
            With List_requi
                If .SelectedIndices.Count > 0 Then
                    For i As Integer = 0 To .SelectedIndices.Count - 1
                        n = CInt(.Items(.SelectedIndices(i)).SubItems(4).Text)
                    Next
                    If MsgBox("Voulez Vous Vraiment Supprimer", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Supprission !") = MsgBoxResult.Yes Then
                        Try
                            Cmd = New SqlCommand("Delete From Clients_Paiement_demande Where id =" & n, Con)
                            open()
                            Cmd.ExecuteNonQuery()
                            cnClose()

                            MsgBox("Montant Supprimer", MsgBoxStyle.Information, "R�ussi !")
                            txt_remarq_requi.Clear()
                            txt_remis.Clear()
                            affiche_requis()
                            total_mont_loc()
                            total_requis()
                            reste()
                        Catch ex As Exception
                            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                            cnClose()
                        End Try
                    End If
                Else
                    MsgBox("Vous n'avez selectionner aucun ligne !", MsgBoxStyle.Critical, "R�ssayer !")
                End If
            End With
        End If

    End Sub

    Private Sub List_requi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_requi.SelectedIndexChanged
        If Radio_loc.Checked = True Then
            Dim n As Integer
            With List_requi
                If .SelectedIndices.Count > 0 Then
                    For i As Integer = 0 To .SelectedIndices.Count - 1
                        n = CInt(.Items(.SelectedIndices(i)).SubItems(4).Text)

                    Next
                End If
            End With
            Try
                Cmd = New SqlCommand("Select * From Clients_Paiement_loc where id =" & n, Con)
                open()
                Dr = Cmd.ExecuteReader()
                Dr.Read()
                If Dr.HasRows Then
                    txt_mont_requi.Text = Dr("Montant")
                    Date_requi.Text = Dr("Date_m")
                    txt_remarq_requi.Text = Dr("Remarque")
                    txt_remis.Text = Dr("Remis")
                End If


                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            '-------------------------------------------------
        ElseIf Radio_Eve.Checked = True Then

            Dim n As Integer
            With List_requi
                If .SelectedIndices.Count > 0 Then
                    For i As Integer = 0 To .SelectedIndices.Count - 1
                        n = CInt(.Items(.SelectedIndices(i)).SubItems(4).Text)

                    Next
                End If
            End With
            Try
                Cmd = New SqlCommand("Select * From Clients_Paiement_demande where id =" & n, Con)
                open()
                Dr = Cmd.ExecuteReader()
                Dr.Read()
                If Dr.HasRows Then
                    txt_mont_requi.Text = Dr("Montant")
                    Date_requi.Text = Dr("Date_m")
                    txt_remarq_requi.Text = Dr("Remarque")
                    txt_remis.Text = Dr("Remis")
                End If


                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try

        End If
    End Sub

    Private Sub Radio_Eve_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Radio_Eve.CheckedChanged
        If Con.State = ConnectionState.Open Then
            cnClose()
        End If

        Group_Eve.Enabled = True
        Group_Loc.Enabled = False
        vider()
        '----------------------------------
        comb_client.Items.Clear()
        comb_client.Text = ""
        Comb_n_demande.Items.Clear()
        Comb_n_demande.Text = ""

        txt_mont_requi.Text = ""


    End Sub

    Private Sub Radio_loc_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Radio_loc.CheckedChanged

        Group_Eve.Enabled = False
        Group_Loc.Enabled = True
        vider()
        '---------------------------
        Try
            comb_client.Items.Clear()
            Cmd = New SqlCommand("Select * From Clients where Location='Oui'", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()
                If Dr("Type_Client") = "Entreprise" Then
                    comb_client.Items.Add(Dr("Nom_entreprise"))
                Else
                    comb_client.Items.Add(Dr("Nom") & "  " & Dr("Prenom"))
                End If
            Loop
            Dr.Close()
            cnClose()
            If comb_client.Items.Count > 0 Then
                comb_client.SelectedIndex = 0
            End If
        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        Me.Enabled = False
        Frm_Recherche_clients.Show()
        rech = "paiem"
    End Sub

    Private Sub comb_client_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comb_client.SelectedIndexChanged
        If Con.State = ConnectionState.Open Then
            cnClose()
        End If
        Dim k As Integer = 0

        Try
            Comb_n_demande.Items.Clear()
            Comb_n_demande.Text = ""
            txt_mont_requi.Clear()
            Cmd = New SqlCommand("select N_demande from Clients,Clients_Demande_Loc where Clients.Id_Client=Clients_Demande_Loc.Id_Client and Location='Oui' and (Nom +'  '+ Prenom) ='" & comb_client.SelectedItem & "'", Con)
            open()
            Dr = Cmd.ExecuteReader()
            If Dr.HasRows Then
                Do While Dr.Read
                    Comb_n_demande.Items.Add(Format(Dr("N_demande")))
                Loop
            End If
            If Comb_n_demande.Items.Count > 0 Then
                Comb_n_demande.SelectedIndex = 0
            End If
            Dr.Close()
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub

    Private Sub Comb_n_demande_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Comb_n_demande.KeyPress
        e.Handled = True
    End Sub



    Private Sub Comb_sortie_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_n_demande.SelectedIndexChanged
        total_mont_loc()
        affiche_requis()
        total_requis()
        total_paye()
        affiche_paye()
        reste()
    End Sub

    Private Sub txt_remis_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_remis.TextChanged
        Dim selStart As Integer = txt_remis.SelectionStart
        Dim selMoveLeft As Integer = 0
        Dim newStr As String = "" 'Build a new string by copying each valid character from the existing string. The new string starts as blank and valid characters are added 1 at a time.

        For i As Integer = 0 To txt_remis.Text.Length - 1

            If "0123456789,".IndexOf(txt_remis.Text(i)) <> -1 Then 'Characters that are in the allowed set will be added to the new string.
                newStr = newStr & txt_remis.Text(i)

            ElseIf i < selStart Then 'Characters that are not valid are removed - if these characters are before the cursor, we need to move the cursor left to account for their removal.
                selMoveLeft = selMoveLeft + 1

            End If
        Next

        txt_remis.Text = newStr 'Place the new text into the textbox.
        txt_remis.SelectionStart = selStart - selMoveLeft 'Move the cursor to the appropriate location.


        If txt_remis.Text = "" Then
            txt_remis.Text = 0
        End If

    End Sub

    Private Sub btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_rech.Click
        If Radio_Eve.Checked = True Then
            Frm_liste_Demande.MdiParent = Frm_Main
            Frm_liste_Demande.Show()
            liste = "loc"
        End If
    End Sub



    Private Sub txt_client_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_client.TextChanged
        If txt_client.Text <> "" Then
            total_mont_loc()
            affiche_requis()
            total_requis()
            total_paye()
            affiche_paye()
            reste()
        End If
    End Sub

    Private Sub txt_mont_paye_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_mont_paye.TextChanged
        Dim selStart As Integer = txt_mont_paye.SelectionStart
        Dim selMoveLeft As Integer = 0
        Dim newStr As String = "" 'Build a new string by copying each valid character from the existing string. The new string starts as blank and valid characters are added 1 at a time.

        For i As Integer = 0 To txt_mont_paye.Text.Length - 1

            If "0123456789,".IndexOf(txt_mont_paye.Text(i)) <> -1 Then 'Characters that are in the allowed set will be added to the new string.
                newStr = newStr & txt_mont_paye.Text(i)

            ElseIf i < selStart Then 'Characters that are not valid are removed - if these characters are before the cursor, we need to move the cursor left to account for their removal.
                selMoveLeft = selMoveLeft + 1

            End If
        Next

        txt_mont_paye.Text = newStr 'Place the new text into the textbox.
        txt_mont_paye.SelectionStart = selStart - selMoveLeft 'Move the cursor to the appropriate location.
    End Sub

    Private Sub cmbTypePay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTypePay.SelectedIndexChanged
        If cmbTypePay.SelectedIndex = 1 Then
            btnCheqFrm.Visible = True
        Else
            btnCheqFrm.Visible = False
        End If

        If cmbTypePay.SelectedIndex = 2 Then
            btnEffet.Visible = True
        Else
            btnEffet.Visible = False
        End If

        If cmbTypePay.SelectedIndex = 3 Then
            btnVirementFrm.Visible = True
        Else
            btnVirementFrm.Visible = False
        End If
    End Sub

    Private Sub btnCheqFrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheqFrm.Click
        'mes modifications1
        Try
            If Radio_loc.Checked Then
                nomClient = comb_client.Text
            End If
            If List_paye.SelectedItems.Count <= 0 Then
                cheq = Nothing
                nomin = Nothing
            End If
            Me.Enabled = False
            frm = Me
            frmCheques.Show()
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            cnClose()
        End Try
    End Sub

    Private Sub btnEffet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEffet.Click
        Try
            If Radio_loc.Checked Then
                nomClient = comb_client.Text
            End If

            If List_paye.SelectedItems.Count <= 0 Then
                effetObj = Nothing
                nomin = Nothing
            End If
            Me.Enabled = False
            frm = Me
            FrmEffet.Show()
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            cnClose()
        End Try
    End Sub

    Private Sub btnVirementFrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVirementFrm.Click
        Try
            If List_paye.SelectedItems.Count <= 0 Then
                objVirement = Nothing
            End If
            Me.Enabled = False
            frm = Me
            frmVirement.Show()
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            cnClose()
        End Try
    End Sub
End Class